package com.blogs.service;

import java.net.URLEncoder;

import android.util.Log;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.entity.SendIngEntry;
import com.blogs.service.GetMsgTask.onTaskCallBack;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public class SendIng {
	public interface IMsgCallBack {
		public void onSuccess(SendIngEntry sce);

		public void onFailed();
	}

	private String url = "";
	private GetMsgTask getresult;
	private IMsgCallBack callback;
	public SendIng(String sid,int ContentId,int ReplyTo,int ParentCommentId,String Content,IMsgCallBack callback) {
		this.callback = callback;
		Content= URLEncoder.encode(Content);
		if(ContentId==0)
		{
			
			this.url=AppConfig.SEND_ING_PUBLIC.replace("{0}", sid).replace("{1}", Content);
		}
		else
		{
			this.url=AppConfig.SEND_ING_COM.replace("{0}", sid).replace("{1}", ContentId+"").replace("{2}", ReplyTo+"").replace("{3}", ParentCommentId+"").replace("{4}", Content);
		}
		getresult = new GetMsgTask(taskCallBack);
	}
	public void Send()
	{
		getresult.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack = new onTaskCallBack() {

		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<SendIngEntry> msg = null;
			try {
				msg = gson.fromJson(result, new TypeToken<MsgEntity<SendIngEntry>>(){}.getType());
			} catch (Exception e) {
				callback.onFailed();
				return;
			}
			SendIngEntry sce=msg.data;
			callback.onSuccess(sce);
		}

		public void onFailed() {
			// TODO Auto-generated method stub
			callback.onFailed();
		}
	};
}
